<template>
  <p-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <p-spin :spinning="confirmLoading">
      <p-form :form="form">
        <p-row>
          <p-col :span="24">
            <p-form-item label="产品编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <f-popup
                contextPath="basic-business"
                v-decorator="['mcode', validatorRules.mcode]"
                :trigger-change="true"
                org-fields="id,mcode,mname,sprc,mexplain"
                dest-fields="mid,mcode,mname,sprc,mexplain"
                code="wms_cpinfo"
                @callback="popupCallback"
              />
            </p-form-item>
          </p-col>
          <p-col :span="24">
            <p-form-item label="产品名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input
                readonly
                v-decorator="[ 'mname', validatorRules.mname]"
                placeholder="请输入产品名称"
              ></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="24">
            <p-form-item label="外置RCU软件名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input
                v-decorator="[ 'outRcuName', validatorRules.outRcuName]"
                placeholder="请输入外置RCU软件名称"
              ></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="24">
            <p-form-item label="外置RCU软件版本" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input
                v-decorator="[ 'outRcuVersion', validatorRules.outRcuVersion]"
                placeholder="请输入外置RCU软件版本"
              ></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="24">
            <p-form-item label="外置RCU软件字段字符" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input
                v-decorator="[ 'outRcuFieldCharacter', validatorRules.outRcuFieldCharacter]"
                placeholder="请输入外置RCU软件字段字符"
              ></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="24">
            <p-form-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input v-decorator="[ 'remark', validatorRules.remark]" placeholder="请输入备注"></p-input>
            </p-form-item>
          </p-col>
        </p-row>
      </p-form>
    </p-spin>
  </p-modal>
</template>

<script>
import { httpAction } from "@/api/manage";
import { SubmitOnchangMixin } from "@/mixins/SubmitOnchangMixin";
import pick from "lodash.pick";

export default {
  name: "MesBcRcuOutAModal",
  mixins: [SubmitOnchangMixin],
  components: {},
  // props:{
  //   mainId:{
  //     type:String,
  //     required:false,
  //     default:''
  //   }
  // },
  data() {
    return {
      form: this.$form.createForm(this),
      title: "操作",
      width: 800,
      visible: false,
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },

      confirmLoading: false,
      validatorRules: {
        mcode: { rules: [{ required: true, message: "请输入产品编码!" }] },
        mname: { rules: [{ required: true, message: "请输入产品名称!" }] },
        outRcuName: {
          rules: [{ required: true, message: "请输入外置RCU软件名称!" }],
        },
        outRcuVersion: {
          rules: [{ required: true, message: "请输入外置RCU软件版本!" }],
        },
        outRcuFieldCharacter: {
          rules: [{ required: true, message: "请输入外置RCU软件字段字符!" }],
        },
        remark: {},
      },
      url: {
        add: "/mes-code/mesBcRcuOutA",
        edit: "/mes-code/mesBcRcuOutA",
      },
    };
  },
  created() {},
  methods: {
    add() {
      this.edit({});
    },
    edit(record) {
      this.form.resetFields();
      this.model = Object.assign({}, record);
      this.visible = true;
      this.$nextTick(() => {
        this.form.setFieldsValue(
          pick(
            this.model,
            "timestamp",
            "companyId",
            "factoryId",
            "clientId",
            "substituteId",
            "flag",
            "mesBcFieldId",
            "mid",
            "mcode",
            "mname",
            "sprc",
            "mexplain",
            "outRcuName",
            "outRcuVersion",
            "outRcuFieldCharacter",
            "remark",
            "createBy",
            "createTime",
            "updateBy",
            "updateTime"
          )
        );
      });
    },
    close() {
      this.$emit("close");
      this.visible = false;
    },
    handleOk() {
      const that = this;
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true;
          let httpurl = "";
          let method = "";
          let formData = Object.assign(this.model, values);
          this.formData = Object.assign(this.model, values);
          // formData['mesBcFieldId'] = this.mainId
          // this.formData['mesBcFieldId'] = this.mainId
          //新增触发
          if (!this.model.id) {
            httpurl += this.url.add;
            method = "post";
            if (this.EnhanceJS.afterAdd) {
              //新增之前
              let Obj = eval(this.EnhanceJS.afterAdd);
              new Obj(this);
            } else {
              console.log("表单提交数据", formData);
              that.modalFrom(httpurl, formData, method);
            }
          } else {
            //编辑触发
            httpurl += this.url.edit;
            method = "put";
            if (this.EnhanceJS.afterEdit) {
              //编辑之前
              let Obj = eval(this.EnhanceJS.afterEdit);
              new Obj(this);
            } else {
              console.log("表单提交数据", formData);
              that.modalFrom(httpurl, formData, method);
            }
          }
        }
      });
    },
    handleCancel() {
      this.close();
    },
    popupCallback(row) {
      this.form.setFieldsValue(
        pick(
          row,
          "timestamp",
          "companyId",
          "factoryId",
          "clientId",
          "substituteId",
          "flag",
          "mesBcFieldId",
          "mid",
          "mcode",
          "mname",
          "sprc",
          "mexplain",
          "outRcuName",
          "outRcuVersion",
          "outRcuFieldCharacter",
          "remark",
          "createBy",
          "createTime",
          "updateBy",
          "updateTime"
        )
      );
    },
  },
};
</script>
